package com.xc.freeapp.redis;

import org.apache.log4j.Logger;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.stereotype.Service;

import com.xc.freeapp.dao.AbstractBaseRedisDao;
@Service
public class VerificationCodeDaoImpl extends AbstractBaseRedisDao<String, String> implements VerificationCodeDao {
	private Logger log = Logger.getLogger(this.getClass());
	public boolean put(final String telphone,final String verCode) {
		 boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {  
	            public Boolean doInRedis(RedisConnection connection)  
	                    throws DataAccessException {  
	                RedisSerializer<String> serializer = getRedisSerializer();  
	                byte[] key  = serializer.serialize(telphone);  
	                byte[] value = serializer.serialize(verCode); 
	                connection.setEx(key, 600, value);
	                return true;
	            }  
	        });  
		return result;
	}
	public boolean put(final String telphone,final String verCode,final long time) {
		 boolean result = redisTemplate.execute(new RedisCallback<Boolean>() {  
	            public Boolean doInRedis(RedisConnection connection)  
	                    throws DataAccessException {  
	                RedisSerializer<String> serializer = getRedisSerializer();  
	                byte[] key  = serializer.serialize(telphone);  
	                byte[] value = serializer.serialize(verCode); 
	                connection.setEx(key, time, value);
	                return true;
	            }  
	        });  
		return result;
	}

	public String get(final String telphone) {
		String  result = redisTemplate.execute(new RedisCallback<String>() {  
	            public String doInRedis(RedisConnection connection)  
	                    throws DataAccessException {  
	                RedisSerializer<String> serializer = getRedisSerializer();  
	                byte[] key = serializer.serialize(telphone);  
	                byte[] value = connection.get(key); 
	                if (value == null) {  
	                    return null;  
	                }  
	                return serializer.deserialize(value);  
	            }  
	        });  
		return result;
	}
	public String delete(final String telphone) {
		String  result = redisTemplate.execute(new RedisCallback<String>() {  
	            public String doInRedis(RedisConnection connection)  
	                    throws DataAccessException {  
	                RedisSerializer<String> serializer = getRedisSerializer();  
	                byte[] key = serializer.serialize(telphone);  
	                byte[] value = serializer.serialize("");  
	                connection.setEx(key, 1, value);
	                if (value == null) {  
	                    return null;  
	                }  
	                return serializer.deserialize(value);  
	            }  
	        });  
		return result;
	}
	
}
